{include file="public/header296" /}
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-list" data-type="insert">添加</button>
      </div>
      <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
      <script type="text/html" id="table-content-list">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
      </script>
    </div>
  </div>
</div>
<script>
  layui.use(['layer','table','laydate','form', 'element'], function(){
    var $ = layui.$
          table = layui.table
          ,laydate  = layui.laydate
          ,form = layui.form;

    table.render({
      elem: '#LAY-app-content-list'
      ,url: "{:url('page/getPageList')}"
      , request: {
        pageName: 'page',
        limitName: 'page_size'
      }
      , page: {
        layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
        ,first: '首页'
        ,last: '尾页'
        ,limit: 10
      }
      ,cols: [[
        {field: 'id', width: 80, title: 'ID', align:'center'}
        ,{field: 'title', title:'标题',align:'center'}
        ,{field: 'status', title: "状态", align:'center', templet: function (d) {
          var is_checked = '';
          if (d.status) {
              is_checked = 'checked';
          }
          return '<input type="checkbox" name="status" switch_page_id="'+ d.id +'" value="'+ d.status +'" lay-skin="switch" lay-text="是|否" lay-filter="status" '+ is_checked +'>';
        }}
        ,{field: 'update_time', title: '最后修改', align:'center'}
        ,{title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#table-content-list'}
      ]]
      ,text: {
        none: '暂无相关数据'
      }
    });
    // 监听工具条
    table.on('tool(LAY-app-content-list)', function(obj){
      var data = obj.data;
      var id = data.id;
      if(obj.event === 'edit'){
        var index = layer.open({
          type: 2
          ,title: '编辑内容'
          ,content: "{:url('page/edit')}?id=" + id
          , maxmin: true
          , area: ['80%', '80%']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
            // 监听提交
            iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function(data){
              var field = data.field;
              $.post("{:url('page/edit')}", {id:field.id, title:field.title, content:field.content}, function (res) {
                if (res.code == 0) {
                  layer.msg(res.msg, {icon: 1});
                  table.reload('LAY-app-content-list');
                  layer.close(index);
                } else {
                  layer.msg(res.msg, {icon: 5});
                }
              });
            });
            submit.trigger('click');
          }
        });
        layer.full(index);
      }
    });
    // 监听开关事件
    form.on('switch(status)', function(obj){
      var id = obj.elem.attributes['switch_page_id'].nodeValue;
      var sta;
      if (obj.elem.checked == true){
        sta = 1;
      } else if(obj.elem.checked == false){
        sta = 0;
      }
      $.post("{:url('page/updatePageStatus')}", {id:id, status:sta}, function (res) {
        if (res.code == 0) {
          layer.msg(res.msg, {icon: 1});
        } else {
          layer.msg(res.msg, {icon: 5});
        }
      });
    });

    var $ = layui.$, active = {
      insert: function(){
        var index = layer.open({
          type: 2
          ,title: '添加内容'
          ,content: "{:url('page/insert')}"
          ,maxmin: true
          ,area: ['550px', '550px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
            iframeWindow.layui.form.on('submit(layuiadmin-app-form-submit)', function(data){
              var field = data.field;

              if(field.name == ''){ layer.msg('标题不能为空',{icon:5}); return false }
              if(field.content == ''){ layer.msg('内容不能为空',{icon:5}); return false }
              $.ajax({
                url: "{:url('page/insert')}",
                type: 'post',
                data: {'field':field},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                    layer.close(index);
                    table.reload('LAY-app-content-list');
                  } else{
                    layer.msg(res.msg,{icon:5});
                    return false;
                  }
                }
              });
            });
            submit.trigger('click');
          }
        });
        layer.full(index);
      },
    };

    // 监听 data-type 事件
    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
</script>
{include file="public/footer" /}